package www.dianbaozu.com.ui.manage

import android.view.View
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import androidx.fragment.app.viewModels
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import www.dianbaozu.com.R
import www.dianbaozu.com.databinding.FragmentUpdateServerLogoBinding
import www.wen.com.common.lib.app.BaseApplication
import www.wen.com.common.lib.base.fragment.BaseLoadingFragment
import www.wen.com.common.lib.base.module.BaseApiViewModel
import www.wen.com.common.lib.data.entities.ServerInfo
import www.wen.com.common.lib.extension.glide.setImageUrl
import www.wen.com.common.lib.extension.imgUrl
import www.wen.com.common.lib.extension.openPhotoAlbumAgency
import www.wen.com.common.lib.extension.postObject
import www.wen.com.common.lib.helper.loading.KeyHelper
import www.wen.com.common.lib.helper.loading.KeyHelper.BUNDLE_DATA
import www.wen.com.common.lib.helper.loading.KeyHelper.UPDATE_SERVER_INFORMATION_KEY
import www.wen.com.common.lib.helper.loading.TitleBarUniversalAdapter
import www.wen.com.common.lib.util.SpUtil
import java.io.File


/**
 * @author WEN
 * @Description:更新服务器头像
 * @date 2020//25 17:48
 */
@AndroidEntryPoint
class UpdateServerLogoFragment : BaseLoadingFragment<FragmentUpdateServerLogoBinding>(),
        View.OnClickListener {

    private lateinit var imgFile: File

    private val mUpdateServerLogoViewModel: UpdateServerLogoViewModel by viewModels()

    lateinit var mServerInfo: ServerInfo


    override val viewModel: BaseApiViewModel
        get() = mUpdateServerLogoViewModel

    override fun isEnableLoadingHelper() = true

    override fun getTitleBarUniversalAdapter(): TitleBarUniversalAdapter = TitleBarUniversalAdapter(
            requireActivity(),
            tilte = "更新服务器头像",
            background = ContextCompat.getColor(BaseApplication.CONTEXT, R.color.color1B1D22),
            action = ::titleAction
    )

    override fun getLayoutId(): Int = R.layout.fragment_update_server_logo

    override fun lazyInitView() {
        mServerInfo = requireArguments().getParcelable(BUNDLE_DATA)!!
        dataBinding.run {
            viewModel = mUpdateServerLogoViewModel
            if(mServerInfo.serverLogo.isNotBlank()) {
                strUpLoadText.isVisible = false
                setImageUrl(dataBinding.ivAvatar, mServerInfo.serverLogo.imgUrl())
            }
        }
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.tvCreateServer -> {
                mUpdateServerLogoViewModel.requestUpdateServerInfo(
                        partMap = mapOf("userId" to SpUtil.decodeString(KeyHelper.MMKV_ID)!!.toRequestBody("text/plain".toMediaTypeOrNull())),
                        serverId = mServerInfo.serverId,
                        MultipartBody.Part.createFormData("files", imgFile.name, imgFile.asRequestBody("image/jpeg".toMediaTypeOrNull()))
                ) {
                    //更新服务器信息
                    postObject(UPDATE_SERVER_INFORMATION_KEY, it)
                    actionLeft()
                }
            }
            R.id.slUploadAvatar -> {
                openPhotoAlbumAgency(size = 400) {
                    val imgLocalFile = if (it[0].androidQToPath.isNullOrBlank()) File(it[0].compressPath) else File(it[0].androidQToPath)
                    if (imgLocalFile.exists()) {
                        imgFile = imgLocalFile
                        setImageUrl(dataBinding.ivAvatar, imgFile.absolutePath)
                        mUpdateServerLogoViewModel.mEnableUpdate.set(true)
                        dataBinding.strUpLoadText.isVisible = false
                    }
                }
            }
        }
    }


}



